var SogouWubi = function (phraseArr) {
    this.phraseArr = phraseArr;
}

SogouWubi.prototype.getMainPhraseBuff = function () {
    var pBody = [];
    var i = 0;
    var order = 0;
    var chReg = new RegExp("^([\\u4e00-\\u9fa5]+)$");
    for (var phrase of this.phraseArr) {
        var code = phrase[0];
        var alphaIndex = code.charCodeAt(0) - 97;
        var hasChAndPhrase = this.isExistChAndPhrase(phrase);
        if(code == "zzrq"){
            phrase[1] = "#$year年$month月$day日";
        }else if(code == "zzsj"){
            phrase[1] = "#$year-$month_mm-$day_dd $fullhour_hh:$minute_mm:$second_ss";
        }else if(code == "zzxq"){
            phrase[1] = "#星期$weekday_cn"
        }
        for (i = 1, order = 1; i < phrase.length; i++, order++) {
            var term = phrase[i];
            var sgArr = this.getSingleBuff(code, term, order);
            if (alphaIndex <= 24 && term.length == 1 && !ChStr.isLegalCh(term)) { continue; }
            if (alphaIndex > 24 || !chReg.test(term) || hasChAndPhrase) {
                Array.prototype.push.apply(pBody, sgArr);
            }
        }
    }
    var pHead = [0xff, 0xfe];
    pHead = pHead.concat(pBody);
    return pHead;
}

SogouWubi.prototype.isExistChAndPhrase = function (phrase) {
    var code = phrase[0];
    if (phrase.length > 2 && (code.length == 2 || code.length == 3)) {
        let set = new Set();
        for (let i = 1; i < phrase.length; i++) {
            if (phrase[i].length > 1 || ChStr.isLegalCh(phrase[i])) {
                set.add(phrase[i].length);
            }
        }
        if (set.size >= 2 && set.has(1)) {
            return true;
        }
    }
    return false;
}
SogouWubi.prototype.getSingleBuff = function (code, term, order) {
    var arr = [];
    Array.prototype.push.apply(arr, PhraseUtil.getStrBuff(code));
    Array.prototype.push.apply(arr, PhraseUtil.getStrBuff("," + order + "="));
    Array.prototype.push.apply(arr, PhraseUtil.getStrBuff(term));
    Array.prototype.push.apply(arr, [0x0d, 0x00, 0x0a, 0x00]);
    return arr;
}